﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/CrystalLevelCountAccess
* 类       名: CrystalLevelCountAccess
* 功      能:  TD
* 时      间:  2024-05-06 11:47:58
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Models;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class CrystalLevelCountAccess
    {
        /// <summary>
        /// 根据实体类列表插入多条数据
        /// </summary>
        /// <param name="levelCountList"></param>
        /// <returns></returns>
        public static DataResult AddCrystalLevelCountMany(List<CrystalLevelCountEntity> levelCountList)
        {
            // 1. 根据多条数据插入多条数据
            DataResult result = new DataResult();
            try
            {
                foreach (var single in levelCountList)
                {
                    var insertRes = AddCrystalLevelCountSingle(single);
                    if (!insertRes.State)
                    {
                        result.Message = insertRes.Message;
                        return result;
                    }
                }
                result.State = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 新增一条数据
        /// </summary>
        /// <param name="levelCount"></param>
        /// <returns></returns>
        public static DataResult AddCrystalLevelCountSingle(CrystalLevelCountEntity levelCount)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.CrystalLevelCounts.Add(levelCount);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 根据晶棒编号获取等级和对应等级数量
        /// </summary>
        /// <param name="crystalNumber"></param>
        /// <returns></returns>
        public static DataResult<List<CrystalLevelCountEntity>> GetLevelCountsByCrystalNumber(string crystalNumber)
        {
            DataResult<List<CrystalLevelCountEntity>> result = new DataResult<List<CrystalLevelCountEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.CrystalLevelCounts.Where(data => data.CrystalNumber!.Equals(crystalNumber)).ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        /// <summary>
        /// 根据某个字段的值去删除数据
        /// </summary>
        /// <param name="crystalNumber"></param>
        /// <returns></returns>
        public static DataResult DeleteDataByCrystalNumber(string crystalNumber)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    var entityList = context.CrystalLevelCounts.Where(data => data.CrystalNumber == crystalNumber).ToList();
                    if (entityList.Count > 0)
                    {
                        foreach (var entity in entityList)
                        {
                            context.CrystalLevelCounts.Remove(entity);
                        }
                        context.SaveChanges();
                    }
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
